package com.tanhua.dubbo.server.api;

import com.alibaba.dubbo.config.annotation.Service;
import com.tanhua.dubbo.server.pojo.Sound;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@Service(version = "1.0.0")
public class SoundApiImpl implements SoundApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 保存语音
     *
     * @param sound
     * @return
     */
    @Override
    public String saveSound(Sound sound) {
        this.mongoTemplate.save(sound);
        return sound.getUserId().toString();
    }

    /**
     * 根据userId查询语音
     *
     * @param userId
     * @return
     */
    public List<Sound> querySoundById(Long userId) {
        Query query = new Query(Criteria.where("userId").is(userId));
        return this.mongoTemplate.find(query, Sound.class);
    }

    public List<Sound> querySounds(Long userId) {
        Query query = new Query(Criteria.where("userId").nin(userId));
        List<Sound> sounds = this.mongoTemplate.find(query, Sound.class);
        return sounds;
    }

}